package cn.bingo.yunsj.ascept;

import cn.bingo.yunsj.common.result.Response;
import cn.bingo.yunsj.common.exception.EmException;
import cn.bingo.yunsj.common.exception.OwnException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * spring boot得统一异常处理
 */
@ControllerAdvice
public class HandlerExceptionAdvice {

    private static final Logger logger = LoggerFactory.getLogger(LogAdvice.class);

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object customHandler(Exception e) {
        Response response = null;
        //处理我们自己得异常
        if (e instanceof OwnException) {
            OwnException ownException = (OwnException) e;
            logger.info("异常编码:" + ownException.getCode() + " 异常原因：" + ownException.getMessage());
            response = new Response(ownException.getCode(), ownException.getMessage());
        } else {
            logger.error("异常编码:" + EmException.SYSTEM_EXCPTION.getErrCode() + " 异常原因：" + e.getMessage());
            response = new Response(EmException.SYSTEM_EXCPTION.getErrCode(), e.getMessage());
        }
        return response;
    }
}
